package touchcard;

import java.util.Random;

import triprecorder.BusTrip;
import triprecorder.TrainTrip;
import triprecorder.Trip;


public class TouchCard extends Thread
{
	private final static Random generator = new Random();
	private int nr;
	private Trip trip;

	public TouchCard(int nr){
		this.nr = nr;
	}
	
	public Trip getTrip(){
		return trip;
	}
	
	public void run(){
		int origin = generator.nextInt(5) + 1;
		if(nr % 2 == 0)
			trip = new BusTrip(origin);
		else
			trip = new TrainTrip(origin);

		System.out.println("Starting " + trip.getDescription());

		TCService tcService = TCService.getTCService();

		tcService.touchIn(this);
		try{
			Thread.sleep(generator.nextInt(120) + 1);
		}
		catch(InterruptedException ie){
			return;
		}
		

		if(trip instanceof TrainTrip){
			TrainTrip bt = (TrainTrip) trip;
			bt.setEnd((bt.getStart() + 1)%5 );
		}
		
		tcService.touchOut(this);
		
		System.out.println("Ending " + trip.getDescription());
	}
}
